<!--  -->
<template>
  <div class="admin-users">
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>后台用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="bigbox">
      <el-row>
        <el-col :span="8">
          <div class="grid-content search-container">
            <el-input
              placeholder="请输入内容"
              v-model.trim="searchCont"
              clearable
              class="search-input"
              @clear="searchClear"
            >
            </el-input>
            <el-button
              type="info"
              plain
              icon="el-icon-search"
              @click="getAdmin_users"
            ></el-button>
          </div>
        </el-col>
        <el-col :span="4" style="padding: 0 10px;">
          <el-button type="primary" @click="addUserBtn">添加后台账号</el-button>
        </el-col>
      </el-row>

      <el-table :data="admin_users" stripe border style="width: 100%">
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="admin_username" label="用户名" width="120">
        </el-table-column>
        <el-table-column prop="admin_mobile" label="用户手机号" width="120">
        </el-table-column>
        <el-table-column prop="admin_email" label="用户邮箱" width="120">
        </el-table-column>
        <el-table-column prop="last_login" label="上次登录时间" width="150">
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="150">
        </el-table-column>
        <el-table-column prop="last_ip" label="上次登录IP" width="150">
        </el-table-column>
        <el-table-column prop="create_ip" label="创建IP" width="150">
        </el-table-column>
        <el-table-column label="用户状态">
          <template slot-scope="props">
            <div>
              <el-switch
                style="display: block"
                v-model="props.row.admin_status"
                @change="switchUser(props.row)"
              >
              </el-switch>
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              @click="editUser(scope.row.admin_username)"
              >编辑</el-button
            >
            <el-button
              @click="deleteUser(scope.row.admin_username)"
              type="danger"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageConfig.currentPage"
        :page-sizes="pageConfig.pageSizes"
        :page-size="pageConfig.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageConfig.pageTotal"
      >
      </el-pagination>
    </el-card>
    <!-- 修改地图弹出层 -->
    <el-dialog
      title="用户编辑"
      :visible.sync="dialogConfig.dialogEditUserVisible"
      class="edit-user-dialog"
    >
      <el-form :ref="editUserForm" :label-position="'right'" label-width="90px">
        <el-form-item label="用户名">
          <el-input disabled></el-input> </el-form-item
        ><el-form-item label="用户手机号"> <el-input></el-input> </el-form-item
        ><el-form-item label="用户邮箱"> <el-input></el-input> </el-form-item>
        <el-form-item>
          <el-button @click="diaologConfig.dialogEditUserVisible = false"
            >取消</el-button
          >
          <el-button @click="editUserSubmit(editUserForm)">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';

export default {
  // import引入的组件需要注入到对象中才能使用
  components: {},
  data () {
    // 这里存放数据
    return {
      // 搜索内容
      searchCont: '',
      // 用户数据
      admin_users: [],
      // 分页器参数配置
      pageConfig: {
        pageTotal: 0,
        pageSize: 5,
        pageSizes: [5, 10, 15, 20],
        currentPage: 1
      },
      loadingConfig: {
        target: 'body',
        fullscreen: true,
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.1)',
        text: '拼命加载中'
      },
      dialogConfig: {
        dialogEditUserVisible: false
      }
    };
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    // 获取后台用户
    getAdmin_users () {
      // 打开加载层
      var loading = this.$loading.service(this.loadingConfig);
      console.log(this);
      this.$axios
        .get('/admin_users', {
          params: {
            search: this.searchCont.trim(),
            pagenum: this.pageConfig.currentPage,
            pagesize: this.pageConfig.pageSize
          }
        })
        .then(res => {
          if (res.status === 0) {
            console.log(res);
            // 处理数据
            res.data.forEach(item => {
              if (item.admin_status === 1) {
                item.admin_status = true;
              } else {
                item.damin_status = false;
              }
            });
            // 设置分页
            this.pageConfig.pageTotal = res.total;
            this.pageConfig.currentPage = res.pagenum;
            // 设置分页数据
            this.admin_users = res.data;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(error => {
          // 请求失败
          this.$message.error(error);
        })
        .finally(() => {
          // 关闭加载层
          this.$nextTick(() => {
            loading.close();
          });
        });
    },
    searchClear () {
      this.getAdmin_users();
    },
    editUser (id) {
      // 根据地图ID查找信息
      // 打开加载层
      // var loading = this.$loading.service(this.loadingConfig);
      // 打开弹出层
      this.dialogConfig.dialogEditUserVisible = true;
      // 发送请求
    },
    handleSizeChange (val) {
      // 切换每页条数重新获取数据
      this.pageConfig.pageSize = val;
      this.getAdmin_users();
    },
    handleCurrentChange (val) {
      // 切换页码时重新获取数据
      this.pageConfig.currentPage = val;
      this.getAdmin_users();
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {
    this.getAdmin_users();
  },
  beforeCreate () {}, // 生命周期 - 创建之前
  beforeMount () {}, // 生命周期 - 挂载之前
  beforeUpdate () {}, // 生命周期 - 更新之前
  updated () {}, // 生命周期 - 更新之后
  beforeDestroy () {}, // 生命周期 - 销毁之前
  destroyed () {}, // 生命周期 - 销毁完成
  activated () {} // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="less" scoped>
.bigbox {
  .el-row {
    margin-left: 20px;
  }
  margin-top: 20px;
  padding-top: 20px;
  background-color: #fff;
  .el-pagination {
    padding-bottom: 20px;
    margin-top: 20px;
  }
}
</style>
